<?php

/**
 * This is the model class for table "transaction".
 *
 * The followings are the available columns in table 'transaction':
 * @property integer $trans_id
 * @property integer $trans_status
 * @property integer $trans_type
 * @property integer $buyer_id
 * @property integer $ideality_id
 * @property string $create_datetime
 */
class Transaction extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'transaction';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('trans_status, trans_type, buyer_id, ideality_id', 'required'),
			array('trans_status, trans_type, buyer_id, ideality_id', 'numerical', 'integerOnly'=>true),
            array('create_datetime', 'default', 'value' => date('Y-m-d H:i:s'), 'setOnEmpty' => true, 'on' => 'insert'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('trans_id, trans_status, trans_type, buyer_id, ideality_id, create_datetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
        return array(
            'user'=> array(self::BELONGS_TO,'User','buyer_id'),
            'ideality'=>array(self::BELONGS_TO,'Ideality','ideality_id'),

        );
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'trans_id' => 'Trans',
			'trans_status' => 'Trans Status',
			'trans_type' => 'Trans Type',
			'buyer_id' => 'Buyer',
			'ideality_id' => 'Ideality',
			'create_datetime' => 'Create Datetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('trans_id',$this->trans_id);

		$criteria->compare('trans_status',$this->trans_status);

		$criteria->compare('trans_type',$this->trans_type);

		$criteria->compare('buyer_id',$this->buyer_id);

		$criteria->compare('ideality_id',$this->ideality_id);

		$criteria->compare('create_datetime',$this->create_datetime,true);

		return new CActiveDataProvider('Transaction', array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * @return Transaction the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public static  function getStatusName($status)
    {
        if($status==Param::TRANS_STATUS_BUYING)
            return 'Pending';
        else if($status==Param::TRANS_STATUS_BUYED)
            return 'Accept';
        else if($status==Param::TRANS_STATUS_CANCEL)
            return 'Reject';

        else
            return $status;
    }
    public static  function getTransTypeName($type)
    {
        if($type==Param::TRANS_TYPE_FULL_PRICE)
            return 'Full price';
        else if($type==Param::TRANS_TYPE_PARTIAL_PRICE)
            return 'Partial price';
        else
            return $type;
    }
    public static function getListTranStatus()
    {
        $listStatus= array(Param::TRANS_STATUS_BUYING=>'Pending',Param::TRANS_STATUS_BUYED=>'Accept',Param::TRANS_STATUS_CANCEL=>'Reject');
        return $listStatus;
    }
    public static function getListTranType()
    {
        $listStatus= array(Param::TRANS_TYPE_FULL_PRICE=>'Full price',Param::TRANS_TYPE_PARTIAL_PRICE=>'Partial price');
        return $listStatus;
    }
    public function  getUserName($user_id)
    {
        $user_name = '';
        $data = User::model()->findAll(array('condition' => 'id=' . $user_id,));
        foreach ($data as $row) {
            $user_name = $row['profile']['firstname'].' '.$row['profile']['lastname'];
        }
        return $user_name;
    }
    public static function  getIdealityName($ideality_id)
    {
        $title = '';
        $data = Ideality::model()->findAll(array('condition' => 'id=' . $ideality_id,));
        foreach ($data as $row) {
            $title =$row['title'];
        }
        return $title;
    }
    public static function  getIdeality()
    {
        $list = array();
        $data = Ideality::model()->findAll();
        foreach ($data as $row) {
            $list[$row['id']] =$row['title'];
        }
        return $list;

    }
    public static  function  getUser()
    {
        $list = array();
        $data = User::model()->findAll();
        foreach ($data as $row) {
            $list[$row['id']] = $row['username'];
        }
        return $list;
    }
}